<template>
  <div class="scale-info">
    <basic-container>
      <avue-form
        ref="form"
        v-model="obj"
        :option="option"
        @reset-change="emptytChange"
        @submit="submit"
      >
        <!-- <template  slot="menuForm">
            <el-button @click="tip">自定义按钮</el-button>
        </template> -->
      </avue-form>
    </basic-container>
  </div>
</template>

<script>
import { saveScale, getScale, updateScale, getGradeList } from "@/api/scale";

var DIC = {
  VAILD: [
    {
      label: "是",
      value: 1,
    },
    {
      label: "否",
      value: 0,
    },
  ],
  SEX: [
    {
      label: "男",
      value: "男",
    },
    {
      label: "女",
      value: "女",
    },
    {
      label: "通用",
      value: "通用",
    },
  ],
};
export default {
  name: "ScaleInfo",
  props: {
    gaugeId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      obj: {},
      sizeValue: "small",
    };
  },
  computed: {
    option() {
      return {
        size: this.sizeValue,
        mockBtn: false,
        submitText: "完成",
        printBtn: false,
        column: [
          {
            label: "量表分类",
            prop: "gaugeTypeId",
            type: "select",
            remote: true,
            span: 12,
            props: {
              label: "typeName",
              value: "gaugeTypeId",
            },
            dicUrl: `/api/blade-measure/measure/gaugetype/listAll?typeName={{key}}`,
            rules: [
              {
                required: true,
                message: "请选择量表分类",
                trigger: "blur",
              },
            ],
          },
          {
            label: "量表名称",
            span: 12,
            prop: "title",
            mock: {
              type: "word",
              min: 10,
              max: 30,
            },
            rules: [
              {
                required: true,
                message: "请输入量表名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "编号",
            span: 12,
            prop: "gaugeNo",
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入编号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "测验说明",
            span: 24,
            prop: "remark",
            type: "textarea",
          },
          {
            label: "指导语",
            span: 24,
            prop: "guidanceNote",
            type: "textarea",
          },
          {
            label: "年级阶段",
            prop: "adds",
            type: "checkbox",
            span: 24,
            props: {
              label: "gradeName",
              value: "gradeNo",
            },
            dicUrl: `/api/blade-system/grade/list`,
          },
          {
            label: "性别判断",
            prop: "sex",
            span: 6,
            type: "radio",
            dicData: DIC.SEX,
            mock: {
              type: "dic",
            },
            change({ value, column }) {},
          },
          {
            label: "是否显示",
            prop: "status",
            span: 6,
            type: "radio",
            dicData: DIC.VAILD,
            mock: {
              type: "dic",
            },
            change({ value, column }) {},
          },
        ],
      };
    },
    editMode() {
      return !!this.gaugeId;
    },
  },
  watch: {
    gaugeId: {
      async handler(gaugeId) {
        if (gaugeId) {
          const [
            {
              data: {
                data: { records: gradeList },
              },
            },
            {
              data: { data: scale },
            },
          ] = await Promise.all([
            getGradeList(1, 1000, { gaugeId }),
            getScale({ id: gaugeId }),
          ]);

          scale.adds = gradeList.map(({ includeId }) => includeId);

          this.obj = scale;
        }
      },
      immediate: true,
    },
  },
  mounted() {
    this.obj = {};
  },
  methods: {
    emptytChange() {},
    async submit(form, done) {
      const post = this.editMode ? updateScale : saveScale;

      this.obj.gaugeTypeName = this.obj.$gaugeTypeId;

      this.obj.adds = this.obj.$adds
        .split("|")
        .map((includeName, index) => ({
          includeName,
          includeId: this.obj.adds[index],
        }))
        .filter(({ includeId }) => !!includeId);

      try {
        const { data } = await post(this.obj);
        if (data.data) {
          // this.$router.replace({
          //   name: "量表设置",
          //   params: { gaugeId: data.data.gaugeId }
          // });
          this.$router.push({
            path: "/scale-manage/platform",
          });
        } else {
          this.$message.error("保存失败!");
        }
      } catch (error) {
        this.$message.error("保存失败!", error);
      } finally {
        done();
      }
    },
    tip() {},
  },
};
</script>

<style lang="scss" scoped>
.scale-info {
}
</style>
